﻿using System.Collections.Generic;

namespace RaptorEngine.Threads
{
	public class ChangeBuffer
	{
		public List<ChangeMessage> Messages { get; set; }

		public ChangeBuffer()
		{
			Messages = new List<ChangeMessage>();
		}
		public void Add(ChangeMessage msg)
		{
			bool exists = false;
			bool newData = msg.MessageType == ChangeMessageType.CreateNewRenderData;
            lock (Messages)
            {
                for (int i = 0; i < Messages.Count; i++)
                {
                    // If the varible has been change during the same update, replace it.
                    if (Messages[i].ID == msg.ID && Messages[i].MessageType == msg.MessageType)
                    {
                        Messages[i] = msg;
                        exists = true;
                        break;
                    }
                    // If new renderData already has been added. Don't send a new message.
                    if (newData && Messages[i].MessageType == ChangeMessageType.CreateNewRenderData)
                    {
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                {
                    Messages.Add(msg);
                }
            }
		}

		public void Clear()
		{
			Messages.Clear();
		}
	}
}
